<template>
  <view class="wrap">
    <uv-navbar :fixed="false" title="投研活动签到" autoBack safeAreaInsetTop placeholder :border='false'></uv-navbar>
    <view class="box">
      <view class="base">
        <view class="hd">
          <view class="comp">{{ detail.target_name }}</view>
          <view class="time">{{ timeFormat(detail.start_time_text, 'yyyy-mm-dd hh:MM:ss') }}</view>
        </view>
        <view class="avatasr" v-if="user.userinfo">
          <avatar :src='setUrl(user.userinfo.avatar)' styles='width: 160rpx; height: 160rpx;'></avatar>
        </view>
        <view class="name">{{ user.name }}</view>
        <view class="comp">任职机构：{{ user.organ }}</view>
        <view class="job">您的职务：{{ user.job }}</view>
        <view class="edit" @click="openPopup">
          <image src="../../static/write.png" class="img"></image>
          <text class="font">修改身份信息</text>
        </view>
      </view>
    </view>
    <view class="desc">*请确保身份信息真实，因身份信息不实导致发生侵权行为，活动组织者和约调研平台不承担任何责任。</view>
    <view class="qrCode">
      <button class="btn" hover-class="none" :disabled="disabled" @click="takeQrCode">确认签到</button>
    </view>
    
    <uv-popup ref="popup" mode='center' bgColor='#FFFFFF' round='26rpx' :closeOnClickOverlay='false'>
      <view class="editPopup">
        <view class="bigTitle">修改</view>
        <view class="column">
          <view class="left">任职机构：</view>
          <view class="right">
            <uv-input v-model="opt.organ" placeholder="请输入任职机构" border='surround' autoBlur fontSize='32rpx' color='#000000' placeholderStyle='font-size: 32rpx; color: #D5D5D5'></uv-input>
          </view>
        </view>
        <view class="column mt20">
          <view class="left">职务：</view>
          <view class="right">
            <uv-input v-model="opt.job" placeholder="请输入职务" border='surround' autoBlur fontSize='32rpx' color='#000000' placeholderStyle='font-size: 32rpx; color: #D5D5D5'></uv-input>
          </view>
        </view>
        <view class="foot">
          <view class='btn cancel' @click="close">取消</view>
          <view class='btn confirm' @click="firm">确认</view>
        </view>
      </view>
    </uv-popup>
  </view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { ref } from 'vue';
import { post } from '@/api';
import { setUrl, toast } from "@/common";
import { timeFormat } from '@/uni_modules/uv-ui-tools/libs/function/index.js';

const popup = ref(null);
let id = '';
let detail = ref({});
let user = ref({});
let disabled = ref(false);
let opt = ref({
  organ: '',
  job: '',
});

onLoad((option) => {
  id = option.id;
  getData();
})

function getData(){
  post('api/survey/getSurveyDetail', { survey_id: id }).then(res => {
    if(res.code == 1){
      const data = res.data;
      detail.value = data;
      getMyEnrollInfo();
    }
  })
}
function getMyEnrollInfo(){
  const params = {
    survey_id: id
  }
  post('api/survey/getMyEnrollInfo', params).then(res => {
    if(res.code == 1){
      const data = res.data;
      user.value = data;
    }
  })
}
// 签到
function takeQrCode(){
  disabled.value = true;
  post('api/survey/userSurveySign', { survey_id: id }).then(res => {
    if(res.code == 1){
      toast('签到成功');
      setTimeout(() => {
        uni.navigateBack({
          delta: 1
        });
      })
    }
  })
}
function firm(){
  const params = {
    enroll_id: user.value.id,
    ...opt.value,
    name: user.value.name,
    mobile: user.value.mobile,
    is_out: user.value.is_out
  }
  post('api/survey/editSurveyEnroll', params).then(res => {
    if(res.code == 1){
      const data = res.data;
      toast('修改成功');
      popup.value.close();
      getMyEnrollInfo();
    }
  })
}
function openPopup(){
  opt.value = {
    organ: user.value.organ,
    job: user.value.job,
  }
  popup.value.open();
}
function close(){
  popup.value.close();
  opt.value = {
    organ: '',
    job: '',
  }
}
</script>

<style lang="scss" scoped>
  .editPopup{
    width: 90vw;
    padding: 30rpx;
    .foot{
      display: flex;
      align-items: center;
      justify-content: space-between;
      width: 100%;
      padding-top: 40rpx;
      .btn{
        width: 45%;
        text-align: center;
        font-size: 32rpx;
        &.cancel{
          background-image: inherit;
          border: 1px solid #E43222;
          color: #000000;
        }
        &.confirm{
          
        }
      }
    }
    .bigTitle{
      text-align: center;
      font-size: 32rpx;
      color: #000000;
      padding-bottom: 30rpx;
    }
    .column{
      display: flex;
      align-items: center;
      width: 100%;
      &.mt20{
        margin-top: 30rpx;
      }
      .right{
        flex: 1;
        width: 0;
      }
      .left{
        // width: 200rpx;
      }
    }
  }
  .qrCode{
    padding: 0 30rpx;
    padding-top: 100rpx;
  }
  .desc{
    font-weight: 400;
    font-size: 28rpx;
    color: #E43222;
    padding: 0 30rpx;
    padding-top: 40rpx;
  }
  .box{
    padding: 30rpx;
    .base{
      width: 100%;
      border-radius: 32rpx;
      background-color: #fff;
      overflow: hidden;
      padding-bottom: 40rpx;
      .edit{
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        padding-top: 100rpx;
        .img{
          width: 28rpx;
          height: 28rpx;
        }
        .font{
          font-weight: 400;
          font-size: 28rpx;
          color: #999999;
          padding-left: 8rpx;
        }
      }
      .job{
        font-weight: 400;
        font-size: 28rpx;
        color: #737373;
        text-align: center;
      }
      .comp{
        font-weight: 400;
        font-size: 28rpx;
        color: #737373;
        text-align: center;
        padding-top: 16rpx;
      }
      .name{
        font-weight: 500;
        font-size: 36rpx;
        color: #2B2B2B;
        text-align: center;
        padding-top: 20rpx;
      }
      .avatasr{
        width: 160rpx;
        height: 160rpx;
        margin: 0 auto;
        margin-top: 100rpx;
      }
      .hd{
        background: #E43222;
        height: 190rpx;
        padding: 0 30rpx;
        .time{
          font-weight: 400;
          font-size: 28rpx;
          color: #FFFFFF;
          padding-top: 20rpx;
        }
        .comp{
          font-weight: 500;
          font-size: 32rpx;
          color: #FFFFFF;
          padding-top: 40rpx;
        }
      }
    }
  }
  .wrap{
    width: 100%;
    min-height: 100vh;
    background-color: #F5F5F5;
  }
</style>